/**
 * Created by Ysssssss on 20/12/14.
 */
const app = getApp()
import productOperationSheetBehaviors from './behaviors/productOperationSheet';
import productCheckbox from './behaviors/productCheckbox';
import componentBehavior from '../../../behaviors/component';
import { COMPONENT_OPTIONS } from '../../../constants/common';
const PAGE_SIZE = 10;

Component({
    behaviors: [componentBehavior, productOperationSheetBehaviors, productCheckbox],
    options: {
        ...COMPONENT_OPTIONS,
        pureDataPattern: /^_/, // 指定所有 _ 开头的数据字段为纯数据字段
    },
    properties: {
        // 展示类型
        viewType: {
            type: String,
            value: 'product', // product 商品 | supply 供货商品
        },
        groupId: {
            type: Number,
            optionalTypes: [String],
            value: '',
        },
        searchValue: {
            type: String,
            value: '',
            observer(newValue, oldValue) {
                const { activity, _componentIsLoad } = this.data
                if (!_componentIsLoad || newValue === oldValue) return;
                activity && this.getProductList({
                    pageNo: 1,
                })
            },
        },
        activity: {
            type: Boolean,
            value: false,
            observer(newValue, oldValue) {
                const { _componentIsLoad } = this.data
                if (!_componentIsLoad || newValue === oldValue) return;
                newValue && this.refreshCurrentPage()
            }
        },
        refreshDate: {
            type: Number,
            value: 0,
            observer(newValue, oldValue) {
                const { _componentIsLoad } = this.data
                if (!_componentIsLoad || newValue === oldValue) return;
                newValue && this.refreshCurrentPage()
            }
        },
    },
    data: {
        productList: [],
        pageNo: 1,
        ts: Date.now(),
        triggerTag: false,
        _isLoadingPage: false,
        _loadOver: false,
        _componentIsLoad: false  // observer触发会在ready前，此时bind:function未定义报错
    },
    lifetimes: {
        ready: function() {
            const { activity } = this.data
            activity && this.getProductList({
                pageNo: 1,
            })
            this.data._componentIsLoad = true
            app.event.addListener('productEdit', this.refreshCurrentPage.bind(this))
        },
        detached: function () {
            app.event.removeListener('productEdit', this.refreshCurrentPage.bind(this))
        }
    },
    methods: {
        // 判断是否有进行中的商品获取请求
        _handlePageRequestSending ({ pageNo }) {
            const { _isLoadingPage, _loadOver } = this.data
            if (_isLoadingPage || (_loadOver && pageNo !== 1)) {
                return true
            } else {
                this.setData({
                    _isLoadingPage: true,
                })
                return false
            }
        },
        // 获取商品列表
        getProductList ({ pageNo, pageSize = PAGE_SIZE }) {
            const { groupId, searchValue } = this.data
            let { ts } = this.data
            pageNo === 1 && (ts = Date.now())
            if (this._handlePageRequestSending({ pageNo })) return;
            this.triggerEvent('getList', [{
                groupId,
                keyword: searchValue,
                pageNo,
                pageSize,
                ts,
            }, (res) => {
                this.setData({
                    productList: pageNo === 1 ? res : [...this.data.productList, ...res],
                    pageNo,
                    _isLoadingPage: false,
                    _loadOver: res.length < pageSize,
                })
            }])
        },
        // 下拉刷新screenWidth
        scrollPullRefresh () {
            this.setData({
                triggerTag: true
            })
            this.refreshCurrentPage()
        },
        // 当前页面刷新
        refreshCurrentPage () {
            const { groupId, searchValue, pageNo, activity } = this.data
            let { ts } = this.data
            pageNo === 1 && (ts = Date.now())
            if (!activity) return;
            const params = {
                groupId,
                keyword: searchValue,
                pageNo: 1,
                pageSize: pageNo * PAGE_SIZE,
                ts
            }
            if (this._handlePageRequestSending(params)) {
                this.setData({
                    triggerTag: false
                })
                return
            };
            this.triggerEvent('getList', [params, (res) => {
                this.setData({
                    productList: res,
                    _isLoadingPage: false,
                    _loadOver: res.length < params.pageSize,
                    triggerTag: false
                })
            }])
        },
        // 下一页数据获取
        loadNext () {
            const { pageNo } = this.data
            this.getProductList({
                pageNo: pageNo + 1,
            })
        },
        // 更改佣金比例
        handleChangePercentOff(e) {
            this.triggerEvent('changePercentOff', e.detail);
        },
    }
})